package testCases;

import common.InitAndClose;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

import java.util.*;
import java.util.concurrent.TimeUnit;

// 搜索功能自动化测试
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class SearchTest extends InitAndClose {

    // 直接点击搜索按钮
    @Order(1)
    @Test
    public void searchTestWithoutUsername() throws InterruptedException {
        // 登录
        login();
        // 跳转到主页
        webDriver.get("http://8.130.172.144:8081/client.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);

        // 点击搜索按钮
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.search > button")).click();
        Thread.sleep(1000);
        // 将结果保存在 Set 中
        Set<String> set = new HashSet<>();
        set.add("lisi");
        set.add("wangwu");
        set.add("zhaoliu");

        // 结果做对比
        WebElement ul = webDriver.findElement(By.cssSelector("#add-friend"));
        int len = ul.findElements(By.tagName("li")).size();

        // #add-friend > li:nth-child(?) > h4
        for (int i = 1; i <= len; i++) {
            // 由于前端这部分被 js 隐藏, 所以使用 js 来查找
            String user = ((JavascriptExecutor) webDriver).executeScript(
                    "return document.querySelector(\"#add-friend > li:nth-child(" + i + ") > h4\").innerText;"
            ).toString();
            System.out.println(user);
            if (!set.contains(user)) {
                Assertions.fail("测试不通过");
            }
        }
        System.out.println("测试通过");
    }

    // 使用 enter
    @Order(2)
    @Test
    public void searchTestWithoutUsernameEnter() {
        // 登录
        login();
        // 跳转到主页
        webDriver.get("http://8.130.172.144:8081/client.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);

        // 找到输入框, 并回车
        webDriver.findElement(By.cssSelector("#inputBox")).sendKeys(Keys.ENTER);
        // 将结果保存在 Set 中
        Set<String> set = new HashSet<>();
        set.add("lisi");
        set.add("wangwu");
        set.add("zhaoliu");

        // 结果做对比
        WebElement ul = webDriver.findElement(By.cssSelector("#add-friend"));
        int len = ul.findElements(By.tagName("li")).size();

        // #add-friend > li:nth-child(?) > h4
        for (int i = 1; i <= len; i++) {
            // 由于前端这部分被 js 隐藏, 所以使用 js 来查找
            String user = ((JavascriptExecutor) webDriver).executeScript(
                    "return document.querySelector(\"#add-friend > li:nth-child(" + i + ") > h4\").innerText;"
            ).toString();
            System.out.println(user);
            if (!set.contains(user)) {
                Assertions.fail("测试不通过");
            }
        }
        System.out.println("测试通过");
    }

    // 输入全名搜索和模糊搜索
    @Order(3)
    @ParameterizedTest
    @CsvFileSource(resources = "searchTestCases.csv", numLinesToSkip = 1)
    public void searchTest(String username, String res) {
        // 登录
        login();
        // 跳转到主页
        webDriver.get("http://8.130.172.144:8081/client.html");
        webDriver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

        // 输入 username 并回车
        webDriver.findElement(By.cssSelector("#inputBox")).sendKeys(username, Keys.ENTER);

        // resList 保存结果集
        // actualList 保存测试出来的结果
        List<String> resList = (res == null ? new ArrayList<>() : Arrays.asList(res.split(",")));
        List<String> actualList = new ArrayList<>();

        WebElement ul = webDriver.findElement(By.cssSelector("#add-friend"));
        List<WebElement> li = ul.findElements(By.tagName("li"));
        int len = li.size();

        // #add-friend > li:nth-child(?) > h4
        for (int i = 1; i <= len; i++) {
            // 由于前端这部分被 js 隐藏, 所以使用 js 来查找
            String user = ((JavascriptExecutor) webDriver).executeScript(
                    "return document.querySelector(\"#add-friend > li:nth-child(" + i + ") > h4\").innerText;"
            ).toString();
            actualList.add(user);
            System.out.println(user);
        }

        for (String s : resList) {
            if (!actualList.contains(s.trim())) {
                Assertions.fail("测试不通过, 结果中缺少: " + s);
            }
        }

        System.out.println("测试通过");
    }
}
